package com.qzdsoft.erpcloud.service.proj.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qzdsoft.erpcloud.domain.proj.MaterialAlias;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.domain.sys.User;
import com.qzdsoft.erpcloud.mapper.proj.MaterialAliasMapper;
import com.qzdsoft.erpcloud.mapper.proj.MaterialAliasViMapper;
import com.qzdsoft.erpcloud.service.proj.MaterialAliasService;
import com.qzdsoft.erpcloud.vo.proj.MaterialAliasInfo;
import com.qzdsoft.erpcloud.vo.query.sys.SysMenubuttonQueryParam;
import com.qzdsoft.vo.Page;
import com.qzdsoft.vo.PageRequest;
import com.qzdsoft.vo.Response;

/**
 * Created by Admin
 */
@Service
public class  MaterialAliasServiceImpl implements MaterialAliasService {
	@Autowired
    private MaterialAliasMapper materialAliasMapper;
	@Autowired
	private MaterialAliasViMapper materialAliasViMapper;

	@Override
	public Page<MaterialAliasInfo> findPage(SysMenubuttonQueryParam params, LoginUser loginUser) {
        PageRequest request = new PageRequest(params.getiDisplayStart(), params.getiDisplayLength());
        long count = materialAliasMapper.countMaterialAlias(params.getSearchKey());
        if (count == 0) {
            return new Page<MaterialAliasInfo>(new ArrayList<MaterialAliasInfo>(), count,params.getsEcho(),request);
        }
		 
        List<MaterialAliasInfo> result = materialAliasViMapper.findPageMaterialAlias(request,params.getSearchKey());
        Page<MaterialAliasInfo> materialAlias = new Page<>(result, count,params.getsEcho(),request);
        if (materialAlias.getCurrentPage() > materialAlias.getTotalPage()) {
            request = new PageRequest(materialAlias.getTotalPage(), params.getiDisplayLength());
            result = materialAliasViMapper.findPageMaterialAlias(request,params.getSearchKey());
            materialAlias = new Page<>( result, count,params.getsEcho(),request);
        }
        return materialAlias;
	}
	/**
	 * 新增存货别名
	 */
	@Override
	public Response addMaterialAlias(MaterialAliasInfo tProjMaterialAliasVi, LoginUser loginUser) throws Exception {
		//判断Form 提交是否为空
    	if(tProjMaterialAliasVi==null){
    		throw new Exception("提交数据为空，不可保存！！");
    	}
//    	MaterialAlias tProjMaterialAlias = new MaterialAlias();
//    	if("客户".equals(tProjMaterialAliasVi.getSupplierCustomerType())){
//    		tProjMaterialAlias.setSupplierCustomerType(true);
//    	}else{
//    		tProjMaterialAlias.setSupplierCustomerType(false);
//    	}
    	
    	materialAliasMapper.insert(tProjMaterialAliasVi);
	        return Response.success("新增存货别名成功");
	}
	/**
	 * 根据ID 查询唯一一条物料信息
	 */
	@Override
	public MaterialAliasInfo getById(Integer id) {
		MaterialAliasInfo tProjMaterialAliasVi = new MaterialAliasInfo();
		tProjMaterialAliasVi.setId(id);
		return this.materialAliasViMapper.selectByPrimaryKey(tProjMaterialAliasVi);
	}
	/**
	 * 编辑保存事件
	 */
	@Override
	public Response editMaterialAlias(MaterialAliasInfo tProjMaterialAliasVi, LoginUser loginUser) {
		MaterialAliasInfo searchMaterialAliasVi = this.getById(tProjMaterialAliasVi.getId());
		if(searchMaterialAliasVi==null){
			return  Response.error("存货别名修改失败，当前存货别名信息未找到！！");
		}
		int id = materialAliasMapper.updateByPrimaryKey(tProjMaterialAliasVi);
		MaterialAliasInfo returnEn = this.getById(id);
		return Response.success("存货别名修改成功");
	}
	/**
	 * 删除物料信息
	 */
	@Override
	public Response delteMaterialAlias(Integer id, LoginUser loginUser) {
		MaterialAliasInfo searchMaterialAlias = this.getById(id);
		if(searchMaterialAlias==null){
			return  Response.error("存货别名删除失败，当前存货别名信息未找到！！");
		}
		MaterialAlias tProjMaterialAlias =new MaterialAlias();
		tProjMaterialAlias.setId(id);
		this.materialAliasMapper.deleteByPrimaryKey(tProjMaterialAlias);
		return Response.success("存货别名删除成功");
	}
	
	/**
	 * 获取jqgrid 存货信息数据
	 */
	@Override
	public String getJqGridJsonData(int rows, int page) {
		List<MaterialAliasInfo> materialAliasList = materialAliasViMapper.findAll();
		if(materialAliasList.size()<=0){
			String jsondata = "{\"page\":\"1\"," + " \"total\":2," + " \"records\":\"13\"," + " \"rows\":" + " [" + " {" + " \"id\":\"13\"," + " \"cell\":" + " [\"13\",\"2007-10-06\",\"Client 3\",\"1000.00\",\"0.00\",\"1000.00\",null]" + " }," + " {" + " \"id\":\"12\"," + " \"cell\":" + " [\"12\",\"2007-10-06\",\"Client 2\",\"700.00\",\"140.00\",\"840.00\",null]" + " }," + " {" + " \"id\":\"11\"," + " \"cell\":" + " [\"11\",\"2007-10-06\",\"Client 1\",\"600.00\",\"120.00\",\"720.00\",null]" + " }," + " {" + " \"id\":\"10\"," + " \"cell\":" + " [\"10\",\"2007-10-06\",\"Client 2\",\"100.00\",\"20.00\",\"120.00\",null]" + " }," + " {" + " \"id\":\"9\"," + " \"cell\":" + " [\"9\",\"2007-10-06\",\"Client 1\",\"200.00\",\"40.00\",\"240.00\",null]" + " }," + " {" + " \"id\":\"8\"," + " \"cell\":" + " [\"8\",\"2007-10-06\",\"Client 3\",\"200.00\",\"0.00\",\"200.00\",null]" + " }," + " {" + " \"id\":\"7\"," + " \"cell\":" + " [\"7\",\"2007-10-05\",\"Client 2\",\"120.00\",\"12.00\",\"134.00\",null]" + " }," + " {" + " \"id\":\"6\"," + " \"cell\":" + " [\"6\",\"2007-10-05\",\"Client 1\",\"50.00\",\"10.00\",\"60.00\",\"\"]" + " }," + " {" + " \"id\":\"5\"," + " \"cell\":" + " [\"5\",\"2007-10-05\",\"Client 3\",\"100.00\",\"0.00\",\"100.00\",\"no tax at all\"]" + " }," + " {" + " \"id\":\"4\"," + " \"cell\":" + " [\"4\",\"2007-10-04\",\"Client 3\",\"150.00\",\"0.00\",\"150.00\",\"no tax\"]" + " }" + " ]," + " \"userdata\":{\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"}" + " }";
			return jsondata;
		}else{
			int records = materialAliasList.size();
			int total = records/rows + 1;
			
			StringBuilder sb = new StringBuilder();
			sb.append("{\"page\":").append("\"").append(page).append("\",") 
	            .append("\"total\":").append("\"").append(total).append("\",") 
	            .append("\"records\":").append("\"").append(records).append("\",") 
	            .append("\"rows\":[");
			
			for(int i=(page-1)*rows; (i<records)&&(i<page*rows); i++){
				MaterialAliasInfo fiConfig = materialAliasList.get(i);
//				sb.append(JsonUtils.objectToJson(fiConfig)); 
				int j = i + 1;
				if((j<records)&&(j<page*rows)){
					sb.append(",");
				}
			}
			sb.append("]}");
			System.out.println("&^^*^*^&(&(&*(&*&*T^&^++++="+sb.toString());
			return sb.toString();
		}
	}
	/**
	 * 获取jqGrid 往来单位信息
	 */
	@Override
	public String getPartnerGridJsonData(int rows, int page) {
		List<MaterialAliasInfo> materialAliasList = materialAliasViMapper.findPartnerAll();
		if(materialAliasList.size()<=0){
			String jsondata = "{\"page\":\"1\"," + " \"total\":2," + " \"records\":\"13\"," + " \"rows\":" + " [" + " {" + " \"id\":\"13\"," + " \"cell\":" + " [\"13\",\"2007-10-06\",\"Client 3\",\"1000.00\",\"0.00\",\"1000.00\",null]" + " }," + " {" + " \"id\":\"12\"," + " \"cell\":" + " [\"12\",\"2007-10-06\",\"Client 2\",\"700.00\",\"140.00\",\"840.00\",null]" + " }," + " {" + " \"id\":\"11\"," + " \"cell\":" + " [\"11\",\"2007-10-06\",\"Client 1\",\"600.00\",\"120.00\",\"720.00\",null]" + " }," + " {" + " \"id\":\"10\"," + " \"cell\":" + " [\"10\",\"2007-10-06\",\"Client 2\",\"100.00\",\"20.00\",\"120.00\",null]" + " }," + " {" + " \"id\":\"9\"," + " \"cell\":" + " [\"9\",\"2007-10-06\",\"Client 1\",\"200.00\",\"40.00\",\"240.00\",null]" + " }," + " {" + " \"id\":\"8\"," + " \"cell\":" + " [\"8\",\"2007-10-06\",\"Client 3\",\"200.00\",\"0.00\",\"200.00\",null]" + " }," + " {" + " \"id\":\"7\"," + " \"cell\":" + " [\"7\",\"2007-10-05\",\"Client 2\",\"120.00\",\"12.00\",\"134.00\",null]" + " }," + " {" + " \"id\":\"6\"," + " \"cell\":" + " [\"6\",\"2007-10-05\",\"Client 1\",\"50.00\",\"10.00\",\"60.00\",\"\"]" + " }," + " {" + " \"id\":\"5\"," + " \"cell\":" + " [\"5\",\"2007-10-05\",\"Client 3\",\"100.00\",\"0.00\",\"100.00\",\"no tax at all\"]" + " }," + " {" + " \"id\":\"4\"," + " \"cell\":" + " [\"4\",\"2007-10-04\",\"Client 3\",\"150.00\",\"0.00\",\"150.00\",\"no tax\"]" + " }" + " ]," + " \"userdata\":{\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"}" + " }";
			return jsondata;
		}else{
			int records = materialAliasList.size();
			int total = records/rows + 1;
			
			StringBuilder sb = new StringBuilder();
			sb.append("{\"page\":").append("\"").append(page).append("\",") 
	            .append("\"total\":").append("\"").append(total).append("\",") 
	            .append("\"records\":").append("\"").append(records).append("\",") 
	            .append("\"rows\":[");
		
			for(int i=(page-1)*rows; (i<records)&&(i<page*rows); i++){
				MaterialAliasInfo fiConfig = materialAliasList.get(i);
//				sb.append(JsonUtils.objectToJson(fiConfig));
				int j = i + 1;
				if((j<records)&&(j<page*rows)){
					sb.append(",");
				}
			}
			sb.append("]}");
			System.out.println("&^^*^*^&(&(&*(&*&*T^&^++++="+sb.toString());
			return sb.toString();
		}
	}

}
